package coupon

import (
	"bp/internal/render"
	"bp/internal/service/param"
	"bp/internal/util"
	couponGrpc "bp/rpc/gen/webapi/coupon"
	"context"
)

// GetCoupon 获取优惠券
func (s *CouponWebApiImp) GetCoupon(ctx context.Context, req *couponGrpc.GetCouponReq) (res *couponGrpc.GetCouponRes, err error) {
	slog := util.NewContextLogger(ctx, s.slog)
	slog.Infof("coupon_imp/query.GetCoupon=>%+v", req)
	defer render.FriendlyEntityError(&err, EntityName)
	res = &couponGrpc.GetCouponRes{}
	q := s.dao.Q

	couponID, err := s.VldGetCouponReq(ctx, q, req)
	if err != nil {
		return
	}
	couponPtr, err := s.entityHub.CouponService.GetCouponPtr(ctx, q, couponID)
	if err != nil {
		return
	}
	itemCategoryConfPtrs, err := s.entityHub.CouponService.ListItemCategoryConfigurationPtrsByCouponID(ctx, q, couponID)
	if err != nil {
		return
	}
	itemConfPtrs, err := s.entityHub.CouponService.ListItemConfigurationPtrsByCouponID(ctx, q, couponID)
	if err != nil {
		return
	}
	res.Coupon = s.ConvCouponPtrEntityToPb(itemCategoryConfPtrs, itemConfPtrs)(couponPtr)
	return
}

func (s *CouponWebApiImp) ListCoupons(ctx context.Context, req *couponGrpc.ListCouponsReq) (res *couponGrpc.ListCouponsRes, err error) {
	slog := util.NewContextLogger(ctx, s.slog)
	slog.Infof("coupon_imp/query.ListCoupons=>%+v", req)
	defer render.FriendlyEntityError(&err, EntityName)
	res = &couponGrpc.ListCouponsRes{}
	q := s.dao.Q

	cPtr := param.GenListCommonParamFromContext(ctx)
	ptr, err := s.VldListCouponsReq(ctx, q, req)
	if err != nil {
		return
	}
	total, couponPtrs, err := s.entityHub.CouponService.ListCouponPtrs(ctx, q, cPtr, ptr)
	if err != nil {
		return
	}

	var itemCategoryMap param.EntityIDToItemCategoryConfigurationPtrsMap
	var itemMap param.EntityIDToItemConfigurationPtrsMap
	if req.WithItemCategoryConfs {
		itemCategoryMap, err = s.entityHub.CouponService.GetMapOfCouponIDToItemCategoryConfigurationPtrsByCouponPtrs(ctx, q, couponPtrs)
		if err != nil {
			return
		}
	}
	if req.WithItemConfs {
		itemMap, err = s.entityHub.CouponService.GetMapOfCouponIDToItemConfigurationPtrsByCouponPtrs(ctx, q, couponPtrs)
		if err != nil {
			return
		}
	}
	res.Total = int32(total)
	res.Rows = s.ConvCouponPtrArrayEntityToPb(itemCategoryMap, itemMap)(couponPtrs)
	return
}

func (s *CouponWebApiImp) ListCouponWithStats(ctx context.Context, req *couponGrpc.ListCouponWithStatsReq) (res *couponGrpc.ListCouponWithStatsRes, err error) {
	slog := util.NewContextLogger(ctx, s.slog)
	slog.Infof("coupon_imp/query.ListCouponWithStats=>%+v", req)
	defer render.FriendlyEntityError(&err, EntityName)
	res = &couponGrpc.ListCouponWithStatsRes{}
	q := s.dao.Q

	cPtr := param.GenListCommonParamFromContext(ctx)
	ptr, err := s.VldListCouponWithStatsReq(ctx, q, req)
	if err != nil {
		return
	}
	total, couponWithStatPtrs, err := s.entityHub.CouponService.ListCouponWithStatPtrs(ctx, q, cPtr, ptr)
	if err != nil {
		return
	}
	var itemCategoryMap param.EntityIDToItemCategoryConfigurationPtrsMap
	var itemMap param.EntityIDToItemConfigurationPtrsMap
	if req.WithItemCategoryConfs {
		itemCategoryMap, err = s.entityHub.CouponService.GetMapOfCouponIDToItemCategoryConfigurationPtrsByListCouponWithStatRowPtrs(ctx, q, couponWithStatPtrs)
		if err != nil {
			return
		}
	}
	if req.WithItemConfs {
		itemMap, err = s.entityHub.CouponService.GetMapOfCouponIDToItemConfigurationPtrsByListCouponWithStatRowPtrs(ctx, q, couponWithStatPtrs)
		if err != nil {
			return
		}
	}
	res.Total = int32(total)
	res.Rows = s.ConvCouponWithStatPtrArrayEntityToPb(itemCategoryMap, itemMap)(couponWithStatPtrs)
	return
}
